//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <global.h>
#include <qboxlayout.h>
#include <player/controller.h>
#include <osgGA/TrackballManipulator>
#include <model/root.h>
#include "osgviewerwidget.h"
#include "playerwindow.h"
//
PlayerWindow::PlayerWindow(QWidget *parent,Qt::WindowFlags flags)
:
   QWidget(parent,flags),
   playing(false),
   viewer(new OSGViewerWidget(this,"viewer"))
{
   QHBoxLayout *l=new QHBoxLayout();
   l->addWidget(viewer);
   l->setContentsMargins(0,0,0,0);
   setLayout(l);
   //
   scene_root=new osg::Group();
   scene_background=new osg::Group();
   controller=new player::Controller();
   particle_geode=new osg::Geode();
   scene_root->addChild(scene_background);
   scene_root->addChild(controller);
   scene_root->addChild(particle_geode);
   //
   viewer->get_viewer()->setCameraManipulator(new osgGA::TrackballManipulator);
   viewer->get_viewer()->setSceneData(scene_root);
}
PlayerWindow::~PlayerWindow()
{
}
//
void PlayerWindow::set_playing(bool playing)
{
   if(playing!=this->playing)
   {
      this->playing=playing;
      if(has_model())
      {
         if(playing)
         {
            controller->play();
         }
         else
         {
            controller->pause();
         }
      }
   }
}
void PlayerWindow::set_current_time(double time)
{
   if(has_model()){
      controller->seek(time);
   }
}
void PlayerWindow::set_gravitation_enabled(bool enabled)
{
   controller->set_gravitation_enabled(enabled);
}
void PlayerWindow::set_friction_enabled(bool enabled)
{
   controller->set_friction_enabled(enabled);
}
//
void PlayerWindow::set_model(model::Root* model)
{
   particle_geode->removeDrawables(0,particle_geode->getNumDrawables());
   if(model)
   {
      controller->assign_model(*model);
      controller->add_particle_systems(particle_geode);
      if(playing)
      {
         controller->play();
      }
   }
   else
   {
      controller->deassign_model();
   }
}
bool PlayerWindow::has_model() const
{
   return controller->is_model_assigned();
}
bool PlayerWindow::is_playing() const
{
   return playing;
}
double PlayerWindow::get_current_time() const
{
   if(has_model())
   {
      return controller->get_current_time();
   }
   else
   {
      return 0.0;
   }
}
bool PlayerWindow::get_gravitation_enabled() const
{
   return controller->get_gravitation_enabled();
}
bool PlayerWindow::get_friction_enabled() const
{
   return controller->get_friction_enabled();
}
